package org.hfchunya.mutualHelp.modular.system.service.impl;

import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hfchunya.mutualHelp.core.common.page.LayuiPageFactory;
import org.hfchunya.mutualHelp.core.common.page.LayuiPageInfo;
import org.hfchunya.mutualHelp.modular.system.entity.NewCategroy;
import org.hfchunya.mutualHelp.modular.system.mapper.NewCategroyMapper;
import org.hfchunya.mutualHelp.modular.system.model.params.NewCategroyParam;
import org.hfchunya.mutualHelp.modular.system.model.result.NewCategroyResult;
import org.hfchunya.mutualHelp.modular.system.service.INewCategroyService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 新闻与栏目关联表 服务实现类
 * </p>
 *
 * @author 付道敏
 * @since 2019-05-05
 */
@Service
public class NewCategroyServiceImpl extends ServiceImpl<NewCategroyMapper, NewCategroy> implements INewCategroyService {

    @Override
    public void add(NewCategroyParam param) {
        NewCategroy entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NewCategroyParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(NewCategroyParam param) {
        NewCategroy oldEntity = getOldEntity(param);
        NewCategroy newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NewCategroyResult findBySpec(NewCategroyParam param) {
        return null;
    }

    @Override
    public List<NewCategroyResult> findListBySpec(NewCategroyParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NewCategroyParam param) {
        Page pageContext = getPageContext();
        QueryWrapper<NewCategroy> objectQueryWrapper = new QueryWrapper<>();
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(NewCategroyParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NewCategroy getOldEntity(NewCategroyParam param) {
        return this.getById(getKey(param));
    }

    private NewCategroy getEntity(NewCategroyParam param) {
        NewCategroy entity = new NewCategroy();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
